package com.rongxun.hbwallet;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.rongxun.utils.AppUtils;
import com.rongxun.utils.wallet.AppFilePath;
import com.rongxun.utils.wallet.DaoMaster;
import com.rongxun.utils.wallet.DaoSession;

import java.util.HashSet;
import java.util.Set;

/**
 * @author forza
 * @classname: App
 * @Description:
 * @date 2016/7/12 11:26
 */
public class MyApp extends Application {

    private static MyApp         instance;
    private        Set<Activity> allActivities;
    private        Activity      mCurrentActivity;
    public static int   SCREEN_WIDTH  = -1;
    public static int   SCREEN_HEIGHT = -1;
    public static float DIMEN_RATE    = -1.0F;
    public static int   DIMEN_DPI     = -1;
    private String TAG="MyApp";
    private DaoSession daoSession;


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //初始化屏幕宽高
        getScreenSize();
        registerActivityLifecycleCallbacks(callbacks);
        AppFilePath.init(this);
        initGreenDao();
        AppUtils.init(this);
    }


    private void initGreenDao() {
        //创建数据库表
        DaoMaster.DevOpenHelper mHelper = new DaoMaster.DevOpenHelper(this, "wallet", null);
        SQLiteDatabase db = mHelper.getWritableDatabase();
        daoSession = new DaoMaster(db).newSession();
    }
    public DaoSession getDaoSession() {
        return daoSession;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /* 手机内存不足时的处理 */
    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    /**
     *
     *
     * @return the Application
     */
    public static MyApp getInstance() {
        return instance;
    }




    public void addActivity(Activity act) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }

    public void removeActivity(Activity act) {
        if (allActivities != null) {
            allActivities.remove(act);
        }
    }

    public void exitApp() {
        if (allActivities != null) {
            synchronized (allActivities) {
                for (Activity act : allActivities) {
                    act.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);

    }

    public void getScreenSize() {
        WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        Display display = windowManager.getDefaultDisplay();
        display.getMetrics(dm);
        DIMEN_RATE = dm.density / 1.0F;
        DIMEN_DPI = dm.densityDpi;
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
        if (SCREEN_WIDTH > SCREEN_HEIGHT) {
            int t = SCREEN_HEIGHT;
            SCREEN_HEIGHT = SCREEN_WIDTH;
            SCREEN_WIDTH = t;
        }
    }


    public Object getCurrentActivity() {
        return mCurrentActivity;
    }


    private long appCount;
    private ActivityLifecycleCallbacks callbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {
            mCurrentActivity = activity;
            // 修复InputMethodManager内存泄漏
            appCount++;
        }

        @Override
        public void onActivityResumed(Activity activity) {
        }

        @Override
        public void onActivityPaused(Activity activity) {
        }

        @Override
        public void onActivityStopped(Activity activity) {
            appCount--;

            if (activity == mCurrentActivity) {
                mCurrentActivity = null;
            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    };

    /**
     * app是否在前台
     *
     * @return true前台，false后台
     */
    public boolean isForeground() {
        return appCount > 0;
    }
}
